package com.scs.application.modules.base.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.entity.Dept;
import com.scs.application.modules.base.entity.Staff;
import com.scs.application.modules.base.service.DeptService;
import com.scs.application.modules.base.service.StaffService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

/**
 * <p>
 * 院方人员 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/base/staff")
public class StaffController extends CrudController<StaffService, Staff>{
    @Autowired
    private DeptService deptService;

    /**
     * 根据ID查找数据
     */
    @GetMapping("/find")
    public RestResult find(@RequestParam("id") Serializable id) {
        Staff entity = service.getById(id);
        if (entity == null) {
            return RestResult.error("数据查询失败<br>可能所操作数据已被删除，请刷新后重新尝试操作");
        }
        if (StringUtils.isNotBlank(entity.getOfficeId())) {
            Dept dept = deptService.getOne(Wrappers.<Dept>query().eq("office_id", entity.getOfficeId()), false);
            if (dept != null) {
                entity.setDeptName(dept.getName());
            }
        }
        return RestResult.ok("获取数据成功", entity);
    }
}
